<template>
  <div id="box">
    <el-container>
      <el-header><h1>登录页</h1></el-header>
      <el-main>
        <div class="mainLogin">
          <div class="dm">
            <img
              src="https://img.alicdn.com/tfs/TB1NkOydrus3KVjSZKbXXXqkFXa-720-400.png_q75.jpg"
              class="logo"
              alt=""
            />
          </div>
          <div class="loginForm">
            <!-- ============================================================================手机密码登录 -->
            <h4 class="title">
              大麦网不会以任何理由要求您转账汇款，谨防诈骗。
            </h4>
            <el-tabs type="border-card">
              <el-tab-pane label="密码登录">
                <el-form
                  :model="ruleForm"
                  :rules="rules"
                  ref="ruleForm"
                  label-position="left"
                  label-width="0px"
                  class="login-form"
                >
                  <el-alert
                    title="账号或密码错误！请核对后重新输入"
                    type="error"
                    v-show="Error"
                    class="error"
                  >
                  </el-alert>
                  <el-form-item prop="phone">
                    <el-input
                      prefix-icon="el-icon-user-solid"
                      placeholder="请输入手机号码"
                      v-model="ruleForm.phone"
                      clearable
                    >
                    </el-input>
                  </el-form-item>

                  <el-form-item prop="password">
                    <el-input
                      prefix-icon="el-icon-s-cooperation"
                      placeholder="请输入密码"
                      v-model="ruleForm.password"
                      show-password
                      clearable
                    >
                    </el-input>
                  </el-form-item>

                  <el-form-item style="width: 100%">
                    <el-button
                      class="btn-login"
                      size="medium"
                      type="primary"
                      style="width: 100%"
                      @click="login('ruleForm')"
                    >
                      <span>登 录</span>
                    </el-button>
                  </el-form-item>
                  <el-checkbox v-model="checked" class="AUTO"
                    >下次自动登录</el-checkbox
                  >
                  <el-link type="danger" class="a" @click="wangji"
                    >| 忘记密码?</el-link
                  >
                  <el-link type="warning" class="a" @click="zhuce"
                    >立即注册 |</el-link
                  >
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="短信登录">
                <el-form
                  ref="loginForm"
                  :model="form"
                  :rules="rules"
                  :inline="true"
                >
                  <el-form-item prop="towPhone">
                    <el-input
                      type="text"
                      prefix-icon="el-icon-user-solid"
                      maxlength="11"
                      placeholder="请输入手机号码"
                      style="width: 250px"
                      v-model="form.towPhone"
                    />
                  </el-form-item>
                  <el-form-item prop="code">
                    <el-input
                      type="text"
                      prefix-icon="el-icon-warning"
                      maxlength="6"
                      placeholder="请输入验证码"
                      v-model="form.code"
                      style="width: 130px"
                    />
                    <el-button :disabled="disabled" @click="getCode">{{
                      valiBtn
                    }}</el-button>
                  </el-form-item>
                  <el-form-item>
                    <el-button
                      type="primary"
                      @click.prevent="onSubmit('loginForm')"
                      >登 录</el-button
                    >
                  </el-form-item>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="扫码登录">
                <p>使用微信QQ或大麦手机端登录</p>
                <!-- <p><img src="./erweima.png" class="imgs" alt="" /></p> -->
                <p>打开扫一扫登录</p>
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
      </el-main>
      <el-footer><h1>底部</h1></el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    const validatePhone = (rule, value, callback) => {
      //正则表达式进行验证手机号
      if (!/^1[35789]\d{9}$/.test(value)) {
        callback(new Error("手机号格式不正确"));
      }
      callback();
    };
    // ====================================================================验证码登录
    var checkPhone = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入绑定的手机号码"));
      } else if (
        !/^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(
          value
        )
      ) {
        callback(new Error("请输入正确的手机号码"));
      } else {
        callback();
      }
    };
    var checkCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else if (!/^[0-9]+$/.test(value) || !/^\d{6}$/.test(value)) {
        callback(new Error("请输入6位数字的验证码"));
      } else {
        callback();
      }
    };
    return {
      Error: false,
      ruleForm: {
        phone: "",
        password: "",
      },
      rules: {
        //表单验证规则
        phone: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { min: 11, max: 11, message: "长度在11个字符", trigger: "blur" },
          { required: true, trigger: "blur", validator: validatePhone },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
            trigger: "blur",
          },
        ],
      },
      checked: false,
      // ====================================================================验证码登录
      form: {
        towPhone: "",
        code: "",
      },
      autoLogin: false,
      rules: {
        towPhone: [{ validator: checkPhone, required: true, trigger: "blur" }],
        code: [{ validator: checkCode, required: true, trigger: "blur" }],
      },
      valiBtn: "获取验证码",
      disabled: false,
    };
  },
  methods: {
    // 跳转到首页
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http
            .get("http://localhost:8082/login", { params: this.ruleForm })
            .then((res) => {
              console.log(res.data.length);
              console.log(this.ruleForm);
              if (res.data.length == 1) {
                this.$router.replace({ path: "/payment" });
              } else {
                alert("账号未注册!请注册后登录");
              }
            });
        } else {
          // console.log("error submit!!");
          // return false;
          //  console.log(this.Error);
        }
      });
    },
    // 跳转注册页
    zhuce() {
      this.$router.replace({ path: "/index" });
    },
    // 跳转修改密码页
    wangji() {
      this.$router.replace({ path: "/xiugai" });
    },
    // ====================================================================验证码登录
    //获取验证码 并只验证手机号 是否正确
    getCode() {
      this.$refs["loginForm"].validateField("towPhone", (err) => {
        if (err) {
          console.log("未通过");
          return;
        } else {
          console.log("已通过");
          console.log(this.form.phone);
          this.tackBtn(); //验证码倒数60秒
        }
      });
    },
    tackBtn() {
      //验证码倒数60秒
      let time = 60;
      let timer = setInterval(() => {
        if (time == 0) {
          clearInterval(timer);
          this.valiBtn = "获取验证码";
          this.disabled = false;
        } else {
          this.disabled = true;
          this.valiBtn = time + "秒后重试";
          time--;
        }
      }, 1000);
    },
    onSubmit(formName) {
      //点击登录 验证手机& 验证码是否符合条件
      this.$refs[formName].validate((valid) => {
        // 为表单绑定验证功能
        if (valid) {
          this.$alert("成功", "提示");
        }
      });
    },
  },
  components: {},
};
</script>   

<style scoped>
#box {
  text-align: center;
}
.el-main {
  width: 100%;
  height: 500px;
  /* border: 1px red solid; */
  background: #19073d;
  margin: 0 auto;
}
.mainLogin {
  width: 1000px;
  height: 380px;
  /* border: 1px gold solid; */
  margin: 30px auto;
}
.dm .logo {
  width: 60%;
  height: 380px;
  float: left;
}
.loginForm {
  width: 293px;
  /* border: 1px red solid; */
  float: right;
}
.ip {
  margin: 0 auto;
  left: -10px;
}
.AUTO {
  float: left;
}
.a {
  float: right;
}
.title {
  margin: 0;
  padding: 0;
  /* border: 1px red solid; */
  background: white;
  color: gray;
}
.imgs {
  width: 150px;
}
</style>

